﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hangout.Web.Core.Exceptions
{
    public class ExceptionReporting
    {
        public static void AddAnException(int userId,ClientType type,System.Exception e,ref Model.HangoutDBEntities context)
        {
            System.Exception temp = e;
            Stack<System.Exception> st = new Stack<Exception>();
            st.Push(e);
            while (temp.InnerException != null)
            {
                st.Push(e.InnerException);
                temp = temp.InnerException;
            }

            int? LastInserted = null;

            while (st.Count>0)
            {
                string clienttype = "";
                if (type == ClientType.WindowsAzure)
                {
                    clienttype = "Windows Azure";
                }

                if (type == ClientType.WindowsPhone7)
                {
                    clienttype = "Windows Phone 7";
                }

                if (type == ClientType.Windows8)
                {
                    clienttype = "Windows 8";
                }

                if (type == ClientType.WindowsPhone8)
                {
                    clienttype = "Windows Phone 8";
                }



                LastInserted = InsertException(userId, clienttype, st.Pop(), LastInserted, ref context);
                
                
            }

        }


        public static int AddAnException(int userId, ClientType type,string message,string stackTrace,  ref Model.HangoutDBEntities context)
        {
            
            Model.Exception exception = new Model.Exception();
            if (userId == 0)
            {
                exception.UserId = null;
            }
            else
            {
                exception.UserId = userId;
            }
            exception.ExceptionMessage = message;
            exception.StackTrace = stackTrace;
            string clienttype = "";
            if (type == ClientType.WindowsAzure)
            {
                clienttype = "Windows Azure";
            }

            if (type == ClientType.WindowsPhone7)
            {
                clienttype = "Windows Phone 7";
            }

            if (type == ClientType.Windows8)
            {
                clienttype = "Windows 8";
            }

            if (type == ClientType.WindowsPhone8)
            {
                clienttype = "Windows Phone 8";
            }
            exception.DateTimeStamp = DateTime.Now;
            exception.InnerException = null;

            context.AddToExceptions(exception);

            context.SaveChanges(); //save it to the DB

            return exception.ExceptionID;
        }

        private static int InsertException(int userId, string clientType, System.Exception e,int? InnerExceptionId,ref Model.HangoutDBEntities context)
        {
            Model.Exception exception = new Model.Exception();
            if (userId == 0)
            {
                exception.UserId = null;
            }
            else
            {
                exception.UserId = userId;
            }
            exception.ExceptionMessage = e.Message;
            exception.StackTrace = e.StackTrace;
            exception.ClientType = clientType;
            exception.DateTimeStamp = DateTime.Now;
            if (InnerExceptionId != null)
            {
                exception.InnerException = InnerExceptionId;
            }

            context.AddToExceptions(exception);

            context.SaveChanges(); //save it to the DB

            return exception.ExceptionID;

        }
    }
}